using UnityEngine;
using System.Collections;

/**
 * Script of GUI display management (Select Level Menu).
 * The purpose of this class is to display unlocked levels.
 * @author Benjamin Bruneau
 */
public class SelectLevelManagerScript : MonoBehaviour 
{

	private const string SAVE_MANAGER_NAME = "SaveManager"; // Save Manager GameObject's name
	private const string SELECT_LEVEL_TEXT_PREFIX = "SelectLevelText"; // Select Level Text GameObject's name
	private const string BRONZE = "bronze"; // Bronze Reward
	private const string SILVER = "silver"; // Silver Reward
	private const string GOLD = "gold"; // Gold Reward
	private const string MEDAL_BRONZE = "MedalBronze"; // Medal Bronze Text
	private const string MEDAL_SILVER = "MedalSilver"; // Medal Silver Text
	private const string MEDAL_GOLD = "MedalGold"; // Medal Gold Text
	
	private GameObject _saveManager; // Save manager GameObject
	
	/**
	 * Called on initialization.
	 * @return void
	 */
	void Start () 
	{
	
		//Initialized because this object moves through scenes
		_saveManager = GameObject.Find (SAVE_MANAGER_NAME);
		
		//Gets saved datas
		foreach (ArrayList sublist in _saveManager.GetComponent<SaveManagerScript>()._sequence) 
		{
			
			// If level locked, hide it
			if(!bool.Parse((sublist[1]).ToString()))
			{
				GameObject currentObject = GameObject.Find(SELECT_LEVEL_TEXT_PREFIX+sublist[0]);
				currentObject.GetComponent<DetectClickOnObjectScript>().enabled = false;
				currentObject.GetComponent<BoxCollider>().enabled = false;
				currentObject.GetComponent<MeshRenderer>().enabled = false;
						
			// Else, show rewards
			} else {
				
				string medal = (((ArrayList)_saveManager.GetComponent<SaveManagerScript>()._sequence[int.Parse((sublist[0]).ToString())-1])[3]).ToString();
		
				switch (medal) 
				{
					case	 GOLD : GameObject.Find(MEDAL_GOLD+sublist[0]).GetComponent<MeshRenderer>().enabled = true;
						break;
					case	 SILVER : GameObject.Find(MEDAL_SILVER+sublist[0]).GetComponent<MeshRenderer>().enabled = true;
						break;
					case	 BRONZE : GameObject.Find(MEDAL_BRONZE+sublist[0]).GetComponent<MeshRenderer>().enabled = true;
						break;
					default:
						break;
				}				
			}
		}
	}
}
